from django.core.urlresolvers import reverse_lazy
from django.shortcuts import resolve_url
from django.views.generic import CreateView, ListView, UpdateView, DeleteView, DetailView
from basedata.models import City
from product.forms import HouseSailForm, HouseRentForm, FormSet, Form_Set, SearchForm
from product.models import House


class HouseRentListView(ListView):
    """
     租房列表
    """
    model = House
    template_name = "product/house_rent_list.html"
    paginate_by = 10

    def get(self, request, *args, **kwargs):
        self.form = SearchForm(request.GET)
        return super(HouseRentListView, self).get(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(HouseRentListView, self).get_context_data(**kwargs)
        context['form'] = self.form
        return context

    def get_queryset(self):
        kwargs = {'category':'R'}
        form = self.form
        form.is_valid()
        if form.cleaned_data['name']:
            kwargs['name__contains'] = form.cleaned_data['name']
        return House.objects.filter(**kwargs)


class PublishHouseRentCreateView(CreateView):

    """
     发布租房房源
    """
    form_class = HouseRentForm
    template_name = "product/publish_house_rent.html"
    success_url = reverse_lazy("rh_list")

    def get_context_data(self, **kwargs):
        data=super(PublishHouseRentCreateView,self).get_context_data(**kwargs)
        data['formset'] = FormSet()
        data['object'] = House(city=City(id=0))
        return data

    def post(self, request, *args, **kwargs):
        formset = FormSet(request.POST,request.FILES)
        if formset.is_valid():
            self.form_class.formset = formset.forms
        return super(PublishHouseRentCreateView, self).post(request, *args, **kwargs)


class HouseSailListView(ListView):
    """
     卖房列表
    """
    model = House
    template_name = "product/house_sail_list.html"
    paginate_by = 10

    def get(self, request, *args, **kwargs):
        self.form = SearchForm(request.GET)
        return super(HouseSailListView, self).get(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(HouseSailListView, self).get_context_data(**kwargs)
        context['form'] = self.form
        return context

    def get_queryset(self):
        kwargs = {'category': 'S'}
        form = self.form
        form.is_valid()
        if form.cleaned_data['name']:
            kwargs['name__contains'] = form.cleaned_data['name']
        return House.objects.filter(**kwargs)


class PublishHouseSailCreateView(CreateView):

    """
     发布卖房子房源
    """
    form_class = HouseSailForm
    template_name = "product/publish_house_sail.html"
    success_url = reverse_lazy("sh_list")

    def get_context_data(self, **kwargs):
        data=super(PublishHouseSailCreateView,self).get_context_data(**kwargs)
        data['formset']=FormSet()
        data['object'] = House(city=City(id=0))
        return data

    def post(self, request, *args, **kwargs):
        formset = Form_Set(request.POST,request.FILES)
        if formset.is_valid():
            self.form_class.formset = formset.forms
        return super(PublishHouseSailCreateView, self).post(request, *args, **kwargs)


class HouseRentUpdateView(UpdateView):
    """
     更改租房房源信息
    """
    model = House
    form_class = HouseRentForm
    template_name = 'product/publish_house_rent.html'

    def get_context_data(self, **kwargs):
        data=super(HouseRentUpdateView,self).get_context_data(**kwargs)
        data['formset']=FormSet()
        data['item_list'] = self.object.houseimage_set.all()
        return data

    def post(self, request, *args, **kwargs):
        formset = FormSet(request.POST,request.FILES)
        if formset.is_valid():
            self.form_class.formset = formset.forms
        return super(HouseRentUpdateView, self).post(request, *args, **kwargs)

    def get_queryset(self):
        return House.objects.filter(category='R')

    def get_success_url(self):
        return resolve_url('rh_list')


class HouseSailUpdateView(UpdateView):
    """
    更改卖房房源信息
   """

    model = House
    form_class = HouseSailForm
    template_name = 'product/publish_house_sail.html'

    def get_context_data(self, **kwargs):
        data = super(HouseSailUpdateView, self).get_context_data(**kwargs)
        data['formset'] = FormSet()
        data['item_list'] = self.object.houseimage_set.all()
        return data

    def post(self, request, *args, **kwargs):
        formset = FormSet(request.POST, request.FILES)
        if formset.is_valid():
            self.form_class.formset = formset.forms
        return super(HouseSailUpdateView, self).post(request, *args, **kwargs)

    def get_queryset(self):
        return House.objects.filter(category='S')

    def get_success_url(self):
        return resolve_url('sh_list')


class HouseRentDeleteView(DeleteView):
    model = House
    template_name = 'product/house_delete.html'
    success_url = reverse_lazy('rh_list')


class HouseSailDeleteView(DeleteView):
    model = House
    template_name = 'product/house_delete.html'
    success_url = reverse_lazy('sh_list')



